﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Project_02
{ 
    class Command {
        protected String command;
        public Command() {
            command = "";
        }
        public Command(String command) {
            Match match = Regex.Match(command, "(?<cmd>\\w*)\\s.*");
            if (!match.Success) throw new Exception("Wrong syntax!\nType as: " + this.ToString());
            this.command = match.Groups["cmd"].Value;
        }
        override public String ToString() {
            return "<cmd> <argumentlist>";
        }
        public String GetCommand() {
            return command;
        }
    };
    class BanCommand : Command {
        String username;
        String expire;
        String admin;
        String reason;
        public BanCommand(String command) {
            Match match = Regex.Match(command, "(?<cmd>\\w*)\\s" + 
                                               "(?<username>\\w*)\\s" + 
                                               "(?<year>[0-9][0-9][0-9][0-9])/" + 
                                               "(?<month>[0-1][0-9])/" + 
                                               "(?<day>[0-9][0-9])\\s" + 
                                               "(?<admin>\\w*)\\s" + 
                                               "(?<reason>.*)");
            if (!match.Success) throw new Exception("Wrong syntax!\nType as: " + this.ToString());
            Int32 year = Convert.ToInt32(match.Groups["year"].Value);
            Int32 month = Convert.ToInt32(match.Groups["month"].Value);
            Int32 day = Convert.ToInt32(match.Groups["day"].Value);
            if (year < 2011 || month > 12 || day > 31) throw new Exception("Invalid expire date!\nType as: " + this.ToString());
            this.command     = match.Groups["cmd"].Value;
            this.username    = match.Groups["username"].Value;
            this.expire      = year + "/" + month + "/" + day;
            this.admin       = match.Groups["admin"].Value;
            this.reason      = match.Groups["reason"].Value;
        }
        override public String ToString() {
            return "ban <username> <expire date(YYYY/MM/DD)> <admin> <reason>";
        }
    };
    class SilenceCommand : Command {
        String username;
        String expire;
        String admin;
        String reason;
        public SilenceCommand(String command) {
            Match match = Regex.Match(command, "(?<cmd>\\w*)\\s" + 
                                               "(?<username>\\w*)\\s" + 
                                               "(?<year>[0-9][0-9][0-9][0-9])/" + 
                                               "(?<month>[0-1][0-9])/" + 
                                               "(?<day>[0-9][0-9])\\s" + 
                                               "(?<admin>\\w*)\\s" + 
                                               "(?<reason>.*)");
            if (!match.Success) throw new Exception("Wrong syntax!\nType as: " + this.ToString());
            Int32 year = Convert.ToInt32(match.Groups["year"].Value);
            Int32 month = Convert.ToInt32(match.Groups["month"].Value);
            Int32 day = Convert.ToInt32(match.Groups["day"].Value);
            if (year < 2011 || month > 12 || day > 31) throw new Exception("Invalid expire date!\nType as: " + this.ToString());
            this.command     = match.Groups["cmd"].Value;
            this.username    = match.Groups["username"].Value;
            this.expire      = year + "/" + month + "/" + day;
            this.admin       = match.Groups["admin"].Value;
            this.reason      = match.Groups["reason"].Value;
        }
        override public String ToString() {
            return "silence <username> <expire date(YYYY/MM/DD)> <admin> <reason>";
        }
    };
}
